package com.xdap.keymen.service.dayang;

import com.definesys.mpaas.query.MpaasQuery;
import com.xdap.keymen.entity.dayang.DaYangAccountTitleEntity;
import com.xdap.keymen.entity.dayang.DaYangPointsStatementEntity;
import com.xdap.motor.entity.SnowflakeIdWorker;
import com.xdap.runtime.service.RuntimeAppContextService;
import com.xdap.runtime.service.RuntimeDatasourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DaYangPointStatementService {
    private final RuntimeDatasourceService runtimeDatasourceService;
    private final RuntimeAppContextService runtimeAppContextService;
    private final SnowflakeIdWorker snowflakeIdWorker;

    /**
     * Get point change by id.
     *
     * @param id ID.
     * @return {@link DaYangAccountTitleEntity}
     */
    public DaYangPointsStatementEntity getPointStatementById(String id) {
        String currentTenantId = runtimeAppContextService.getCurrentTenantId();
        MpaasQuery query = runtimeDatasourceService.buildTenantMpaasQuery("xdap_app_" + currentTenantId);
        query.eq("id", id);
        return query.doQueryFirst(DaYangPointsStatementEntity.class);
    }

    /**
     * Update point statement.
     *
     * @param pointStatement {@link DaYangPointsStatementEntity}
     * @return Ture or False.
     */
    public Integer updatePointStatementCallback(DaYangPointsStatementEntity pointStatement) {
        String currentTenantId = runtimeAppContextService.getCurrentTenantId();
        MpaasQuery query = runtimeDatasourceService.buildTenantMpaasQuery("xdap_app_" + currentTenantId);
        query.update("up_flag", pointStatement.getUpFlag());
        query.update("up_error_msg", pointStatement.getUpErrorFlag());
        query.eq("id", pointStatement.getId());
        return query.doUpdate(pointStatement);
    }

}
